﻿using GiveMeMenu.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using GiveMeMenu.Service.Translations;
using ServiceModel = GiveMeMenu.Service.ObjectModel;
using GiveMeMenu.Common.Cache;

namespace GiveMeMenu.Service.DataLayer
{
    public class GMMDataAccessLayer : IGMMDataAccessLayer
    {
        PipeNode<ServiceModel.Dish> DishCache = new PipeNode<ServiceModel.Dish>();
        PipeNode<ServiceModel.Dish> DishTodayCache = new PipeNode<ServiceModel.Dish>();
        PipeNode<ServiceModel.LuTable> LuTableCache = new PipeNode<ServiceModel.LuTable>();
        PipeNode<ServiceModel.LuTypeDish> LuTypeDishCache = new PipeNode<ServiceModel.LuTypeDish>();
        KeyEndNode<int, ServiceModel.OrderInformation> OrderInformationByOrderIdCache = new KeyEndNode<int, ServiceModel.OrderInformation>();

        /// <summary>
        /// Gets all dishs.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<ObjectModel.Dish> GetAllDishs()
        {
            IList<ServiceModel.Dish> result = DishCache.Get();
            if (result != null)
            {
                return result;
            }
            else
            {
                using (var context = new GiveMeMenuContextDataContext())
                {
                    IList<Dish> dishs = context.GetDishes().ToList();
                    IList<ServiceModel.Dish> resultList = dishs.ToDishsObjectModel();
                    DishCache.Add(resultList);
                    return resultList;
                }
            }
        }


        /// <summary>
        /// Removes the order.
        /// </summary>
        /// <param name="OId"></param>
        /// <returns></returns>
        public bool RemoveOrder(int OId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.DeleteOrder(OId);
                if (rowEffect > 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Removes the order information.
        /// </summary>
        /// <param name="OId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveOrderInformation(int OId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.DeleteOrderInformation(OId);
                if (rowEffect > 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets the dish today.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<ObjectModel.Dish> GetDishToday()
        {
            IList<ServiceModel.Dish> result = DishTodayCache.Get();
            if (result != null)
            {
                return result;
            }
            else
            {
                using (var context = new GiveMeMenuContextDataContext())
                {
                    IList<Dish> dishs = context.GetDishToday().ToList();
                    IList<ServiceModel.Dish> resultList = dishs.ToDishsObjectModel();
                    DishTodayCache.Add(resultList);
                    return resultList;
                }
            }
        }


        /// <summary>
        /// Adds the dish.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <param name="name">The name.</param>
        /// <param name="price">The price.</param>
        /// <param name="orderDate">The order date.</param>
        /// <param name="descrption">The descrption.</param>
        /// <param name="discount">The discount.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddDish(ObjectModel.Dish dish)
        {
            int? oId = null;
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.AddDish(ref oId, dish.Name, dish.TypeId, dish.Price, dish.Description, dish.Today, dish.Discount);
                if (oId != null)
                {
                    DishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Adds the order.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <param name="typeId">The type id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddOrder(ServiceModel.Order order)
        {
            int? oId = null;
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.AddOrder(ref oId, order.TableId);
                if (oId != null)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Adds the order information.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <param name="typeId">The type id.</param>
        /// <param name="dishId">The dish id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddOrderInformation(ServiceModel.OrderInformation info)
        {
            int? oId = null;
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.AddOrderInformation(ref oId, info.OrderId, info.DishId);
                if (oId != null)
                {
                    return true;
                }
                return false;
            }
        }


        /// <summary>
        /// Addlus the table.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddluTable(ServiceModel.LuTable luTable)
        {
            int? oId = null;
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.AddluTable(ref oId, luTable.Name);
                if (oId != null)
                {
                    LuTableCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Addlus the type dish.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddluTypeDish(ServiceModel.LuTypeDish luTypeDish)
        {
            int? oId = null;
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.AddluTypeDish(ref oId, luTypeDish.Name);
                if (oId != null)
                {
                    LuTypeDishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Getlus the tables.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<ObjectModel.LuTable> GetluTables()
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                IList<luTable> dishs = context.GetluTables().ToList();
                IList<ServiceModel.LuTable> result = dishs.ToLookupTablesObjectModel();
                LuTableCache.Add(result);
                return result;
            }
        }

        /// <summary>
        /// Getlus the type dish.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<ObjectModel.LuTypeDish> GetluTypeDish()
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                IList<luTypeDish> dishs = context.GetluTypeDishes().ToList();
                IList<ServiceModel.LuTypeDish> result = dishs.ToLookupTypeDishsObjectModel();
                LuTypeDishCache.Add(result);
                return result;
            }
        }

        /// <summary>
        /// Gets the order information by order id.
        /// </summary>
        /// <param name="orderOId">The order O id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<ObjectModel.OrderInformation> GetOrderInformationByOrderId(int orderOId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                IList<GetOrderInformationByOrderIdResult> result = context.GetOrderInformationByOrderId(orderOId).ToList();
                IList<ServiceModel.OrderInformation> actualResult = result.ToOrderInformationObjectModel();
                OrderInformationByOrderIdCache.Add(orderOId, actualResult);
                return actualResult;
            }
        }

        /// <summary>
        /// Removes the dish.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveDish(int oId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.DeleteDish(oId);
                if (rowEffect != 0)
                {
                    DishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Removelus the table.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveluTable(int oId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.DeleteluTable(oId);
                if (rowEffect != 0)
                {
                    LuTableCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Removelus the type dish.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveluTypeDish(int oId)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.DeleteluTypeDish(oId);
                if (rowEffect != 0)
                {
                    LuTypeDishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Updates the dish.
        /// </summary>
        /// <param name="typeId">The type id.</param>
        /// <param name="name">The name.</param>
        /// <param name="price">The price.</param>
        /// <param name="orderDate">The order date.</param>
        /// <param name="description">The description.</param>
        /// <param name="discount">The discount.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateDish(ServiceModel.Dish dish)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.UpdateDish(dish.OId, dish.Name, dish.TypeId, dish.Price, dish.Description, dish.Today, dish.Discount);
                if (rowEffect != 0)
                {
                    DishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Updatelus the table.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateluTable(ServiceModel.LuTable luTable)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.UpdateluTable(luTable.OId, luTable.Name);
                if (rowEffect != 0)
                {
                    LuTableCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Updatelus the type dish.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateluTypeDish(ServiceModel.LuTypeDish luTypeDish)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.UpdateluTypeDish(luTypeDish.OId, luTypeDish.Name);
                if (rowEffect != 0)
                {
                    LuTypeDishCache.ClearCache();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Updates the order.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <param name="tableId">The table id.</param>
        /// <param name="confirmOrder">if set to <c>true</c> [confirm order].</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateOrder(ServiceModel.Order order)
        {
            using (var context = new GiveMeMenuContextDataContext())
            {
                int rowEffect = context.UpdateOrder(order.OId, order.TableId, order.OrderConfirm);
                if (rowEffect != 0)
                {
                    return true;
                }
                return false;
            }
        }

       
    }
}
